<?php
$DEFAULT = "";
elgg_make_sticky_form('addprojects');

$group_project = htmlspecialchars(get_input('group_project', "other", false), ENT_QUOTES, 'UTF-8');
$deadline = get_input('deadline');
$total = get_input('total', $DEFAULT);
$max_student = get_input('max_student', $DEFAULT);
$min_student = get_input('min_student', $DEFAULT);
$group_guid = get_input('group_guid', -1);
$semester = get_input('semester', 'HK1');

$date = new DateTime('now');
$date = date_format($date, 'Y');
$this_year = (int)$date;
$academic_year = get_input('academic_year', $this_year . " - " . $this_year + 1);

if ($group_guid != -1) {
    if ($group_project != $DEFAULT) {
        $projectlists = elgg_get_entities(array(
            'type' => "object",
            'subtype' => "projectlist",
            'container_guid' => $group_guid
        ));

        if (!empty ($projectlists)) {
            foreach ($projectlists as $projectlistvalue) {
                $projectlist = $projectlistvalue;
                break;
            }
            if (strpos($projectlist->group_project, $group_project) === true) {

                // TODO: update deadline
                $ar_groupproject = explode(",", $projectlist->group_project);
                $ar_deadline = explode(",", $projectlist->deadline);
                $ar_semesters = explode(",", $projectlist->semester);
                $ar_academic_years = explode(",", $projectlist->semester);
                $ar_deadline_editted = explode(",", $projectlist->academic_year);

                $found = -1;

                for ($i = 0; $i < count($ar_groupproject); $i++) {
                    $old_semester = $ar_semesters[$i];
                    $old_academic_year = $ar_academic_years[$i];
                    if ($ar_groupproject [$i] == $group_project && $old_semester != $semester && $old_academic_year != $academic_year) {
                        $old_deadline = $ar_deadline [$i];
                        if ($old_deadline != $deadline) {
                            $ar_deadline_editted [$i] = "1";
                        }

                        $str = $ar_deadline_editted[0];
                        for ($i = 1; $i < count($ar_deadline_editted); $i++) {
                            $str .= "," . $ar_deadline_editted [$i];
                        }

                        $projectlist->deadline_editted = $str;

                        $found = $i;

                        break;
                    }
                }

                if ($found == -1){ // have found existed
                    // add new
                    $projectlist->group_project .= ",$group_project";

                    $projectlist->deadline_editted .= ",0";

                    $projectlist->deadline .= ",$deadline";

                    $projectlist->semester .= ",$semester";
                    $projectlist->academic_year .= ",$academic_year";

                    $projectlist->save();
                }

            } else {
                $projectlist->group_project .= ",$group_project";

                $projectlist->deadline_editted .= ",0";

                $projectlist->deadline .= ",$deadline";

                $projectlist->semester .= ",$semester";
                $projectlist->academic_year .= ",$academic_year";

                $projectlist->save();
            }
        } else {
            $projectlist = new ElggObject ();
            $projectlist->subtype = "projectlist";
            $projectlist->access_id = ACCESS_LOGGED_IN;
            $projectlist->group_project = $group_project;
            $projectlist->deadline_editted = "0";
            $projectlist->container_guid = $group_guid;
            $projectlist->deadline = $deadline;
            $projectlist->semester .= $semester;
            $projectlist->academic_year .= $academic_year;
            $projectlist->save();
        }

        for ($i = 0; $i < $total; $i++) {
            $name = get_input("name" . ($i + 1), $DEFAULT);
            $max_group = get_input("max_group" . ($i + 1), 0);
            $desp = get_input("descp" . ($i + 1), 0);

            if ($name != $DEFAULT && $max_group != 0) {
                $project = new ElggObject ();
                $project->subtype = "project";
                $project->title = $name;
                $project->description = $desp;
                $project->max_group = $max_group;
                $project->current_group = 0;
                $project->group_project = $group_project;
                $project->max_student = $max_student;
                $project->min_student = $min_student;
                $project->deadline = $deadline;
                $project->container_guid = $group_guid;
                $project->projectlist_guid = $projectlist->guid;
                $project->access_id = ACCESS_LOGGED_IN;
                $project->semester = $semester;
                $project->academic_year = $academic_year;
                $project->save();
                // add_to_river('river/object/project/create', 'create', elgg_get_logged_in_user_guid(),$project->guid);
            } else {
                register_error(elgg_echo('submana:errors:projectmissing') . $name . $max_group);
                forward(REFERER);
            }
        }

        $group = get_entity($group_guid);
        elgg_clear_sticky_form('addprojects');
        $listproj_url = elgg_get_site_url() . "submana/listpro/$group_guid";

        forward($listproj_url);
    } else {
        register_error(elgg_echo('submana:errors:projectmissing') . $name . $max_group);
    }
} else {
    register_error(elgg_echo('submana:errors:nonegroup'));
    forward(REFERER);
}

